Ext.define('PrjAPLean.view.user.UserMangerPanelViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.usermangerpanelContr',
    
    onPanelClose: function(){
    	this.getView().down('grid').getStore().getProxy().extraParams = {};
    },
    checkIfSelect: function(){
    	var selectArr = this.getView().down('grid').getSelectionModel().getSelection();
    	if(selectArr.length == 0){
    		return false;
    	} else {
    		return true;
    	}
    },
    onQueryClick: function (btn){
    	
        var userGrid = this.getView().down('grid');
        var name = this.getView().down('#name').getValue();
        var userStore = userGrid.getStore(); 
        var extraParams = {username: name};
        userStore.on("beforeload", function (userStore, operation, eOpts) {
					 Ext.apply(userStore.proxy.extraParams, extraParams);
					});
        
        userStore.loadPage(1,{"start":0,"limit":20});
    },
    onRefreshClick: function(){
        var userStore = this.getView().down('grid').getStore(); 
        userStore.reload(); 
    },
    onDataManagerClick: function(){
    	if(!this.checkIfSelect()){
    		Ext.Msg.alert('提示','请先选中一条记录。');
    		return;
    	}
    	var dataWin = Ext.create('PrjAPLean.view.user.DataManagerWin');
    	var userId = this.getView().down('#userId').getValue();
    	var param = {userId : userId};
    	auUserContro.getDimensionByUser(param, function(result){
    		if(result != undefined){
    			dataWin.down('combobox').setValue(result.id);
    		}
    	});
    	dataWin.show();
    },
    onApproveManagerClick: function(){
    	if(!this.checkIfSelect()){
    		Ext.Msg.alert('提示','请先选中一条记录。');
    		return;
    	}
    	var approveWin = Ext.create('PrjAPLean.view.user.ApproveManagerWin');
    	var thisWin = this.getView();
    	
    	approveWin.show();
    },
    onFunctionManagerClick: function(){
    	if(!this.checkIfSelect()){
    		Ext.Msg.alert('提示','请先选中一条记录。');
    		return;
    	}
    	var orgTreeWin = Ext.create('PrjAPLean.view.user.FunctionManagerWin');
    	/*var userId = Ext.ComponentQuery.query('#userId')[0].getValue();
    	var param = {userId : userId};
    	auUserContro.getDimensionByUser(param, function(result){
    		if(result != undefined){
    			dataWin.down('combobox').setValue(result.id);
    		}
    	});
    	dataWin.show();*/
    	orgTreeWin.show();
    },
    onSaveClick: function(){
    	if(!this.checkIfSelect()){
    		Ext.Msg.alert('提示','请先选中一条记录。');
    		return;
    	}
    	var userForm = this.getView().down('form');
    	var userObj = userForm.getValues();
    	var userGrid = this.getView().down('grid');
    	var gridSelected = userGrid.getSelectionModel().getSelection()[0];
    	if(userForm.isValid()){
    		userForm.submit({
        		success: function(form, action){
        			if(action.result.flag == 'success'){
        				userGrid.getStore().reload();
        				userGrid.getSelectionModel().select(gridSelected);
            			Ext.Msg.alert('提示','保存成功。');
        			}else{
        				Ext.Msg.alert('提示', action.result.flag);
        			}
        			
        		},
        		failure: function(){
        			Ext.Msg.alert('提示','保存失败。');
        		}
        	});
    	}
    	
    },
    onAddClick: function(){
    	var newUser = Ext.create('PrjAPLean.model.User',{
    		id: -1,
    		username: 'admin',
    		realname: 'admin',
    		email: 'admin@gmail.com',
    		password: 'admin',
    		valid: 1
    		
    	});
    	var thisGrid = this.getView().down('grid');
    	var gridStore = thisGrid.getStore();
    	gridStore.insert(0, newUser);
    	thisGrid.getSelectionModel().select(0);
    	
    },
    /*onGridSelect: function(){
    	var idValue = this.getView().down('#userId').getValue();
    	console.log(idValue);
    	if(idValue > 0){
    		this.getView().down('#username').setEditable(false);
    	}
    }*/
    onIdValueChange: function(field, newValue){
    	var userNameField = this.getView().down('#username');
    	if(newValue > 0){
    		userNameField.setEditable(false);
    	} else {
    		userNameField.setEditable(true);
    	}
    }
    
   
});
